package cn.ncist.health_service_provider.service.impl;

import cn.ncist.health_interface.service.UserService;
import cn.ncist.health_service_provider.dao.PermissionMapper;
import cn.ncist.health_service_provider.dao.RoleMapper;
import cn.ncist.health_service_provider.dao.UserMapper;
import cn.ncist.pojo.Permission;
import cn.ncist.pojo.Role;
import cn.ncist.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
@DubboService(interfaceClass = UserService.class,timeout = 50000)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public User findUserByUsername(String username) {
        User user = userMapper.findUserByUsername(username);
        if (user == null){
            return null;
        }
        Set<Role> roleSet = roleMapper.findRoleByUsername(user.getId());
        if(roleSet != null && roleSet.size() > 0){
            for (Role role : roleSet) {
                Set<Permission> permissionSet = permissionMapper.findPermissionByRoleId(role.getId());
                if(permissionSet != null && permissionSet.size() > 0) {
                    role.setPermissions(permissionSet);
                }
            }
        }
        user.setRoles(roleSet);
        return user;
    }
}
